package com.iims.finance.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.iims.common.annotation.Log;
import com.iims.common.core.controller.BaseController;
import com.iims.common.core.domain.AjaxResult;
import com.iims.common.core.page.TableDataInfo;
import com.iims.common.enums.BusinessType;
import com.iims.finance.domain.CmisCwInvoice;
import com.iims.finance.service.ICmisCwInvoiceService;
import com.iims.finance.vo.resp.CmisCwInvoiceDetailVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author fanpei
 * @date 2021/7/7 14:17
 */
@Api(tags = "K-开票管理")
//@ApiSort(13)
@RestController
@RequestMapping("/business/invoice")
public class InvoiceController extends BaseController {

    @Autowired
    private ICmisCwInvoiceService invoiceService;

    /**
     * 获取开票列表
     */
    @ApiOperation(value = "分页查询开票列表", response = CmisCwInvoiceDetailVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前记录起始索引(从1开始)", name = "pageNum", paramType = "query", required = true),
            @ApiImplicitParam(value = "每页显示记录数（>0）", name = "pageSize", paramType = "query", required = true),
            @ApiImplicitParam(value = "排序列", name = "orderByColumn", paramType = "query"),
            @ApiImplicitParam(value = "升降序方式：0升序 1降序", name = "isAsc", paramType = "query"),
            @ApiImplicitParam(value = "创建开始时间:format = \"yyyy-MM-dd\"", name = "beginTime", paramType = "query"),
            @ApiImplicitParam(value = "创建结束时间:format = \"yyyy-MM-dd\"", name = "endTime", paramType = "query"),
            @ApiImplicitParam(value = "审核状态,值来源于字典business_common_reviewStatus", name = "reviewStatus", paramType = "query"),
            @ApiImplicitParam(value = "开票人:模糊匹配", name = "invoiceUserCN", paramType = "query"),
            @ApiImplicitParam(value = "审核人:模糊匹配", name = "reviewerCN", paramType = "query"),
            @ApiImplicitParam(value = "发票代码:模糊匹配", name = "invoiceCode", paramType = "query"),
            @ApiImplicitParam(value = "发票号码:模糊匹配", name = "invoiceNo", paramType = "query")
    })
    @PreAuthorize("@ss.hasPermi('business:invoice:list')")
    @GetMapping("/list")
    public TableDataInfo list(@ApiIgnore CmisCwInvoice invoice) throws Exception {
        startPage();
        setSearchTimeRange(invoice);
        setSeqNumPageSizeStart(invoice);
        return getDataTable(invoiceService.getCwInvoiceList(invoice));
    }

    @ApiOperation(value = "获取 [合同] 所属开票列表", response = CmisCwInvoiceDetailVo.class)
    @GetMapping("/listByContract/{contractId}")
    public AjaxResult listByContract(@ApiParam(value = "正式合同Id", required = true) @PathVariable Long contractId) {
        return AjaxResult.success(invoiceService.getContractCwInvoiceList(contractId));
    }

    /**
     * 根据开票编号获取详细信息
     */
    @ApiOperation(value = "根据开票Id获取详细信息", response = CmisCwInvoiceDetailVo.class)
    @ApiImplicitParam(name = "invoiceId", value = "开票Id", paramType = "path", required = true)
    @PreAuthorize("@ss.hasPermi('business:invoice:query')")
    @GetMapping(value = "/{invoiceId}")
    public AjaxResult getInfo(@PathVariable Long invoiceId) {
        return AjaxResult.success(invoiceService.getCwInvoiceDetailById(invoiceId));
    }

    /**
     * 新增开票
     */
    @ApiOperation(value = "新增开票")
    @ApiOperationSupport(ignoreParameters = {"invoiceId", "reviewer", "reviewStatus", "reviewTime", "reviewerCN", "invoiceUserCN"})
    @PreAuthorize("@ss.hasPermi('business:invoice:add')")
    @Log(title = "新增开票", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CmisCwInvoice invoice) {
        invoiceService.insertCwInvoice(invoice);
        return AjaxResult.success();
    }

    /**
     * 修改开票
     */
    @ApiOperation("修改开票")
    @PreAuthorize("@ss.hasPermi('business:invoice:edit')")
    @ApiOperationSupport(ignoreParameters = {"reviewer", "reviewStatus", "reviewTime", "reviewerCN", "invoiceUserCN"})
    @Log(title = "修改开票", businessType = BusinessType.UPDATE)
    @Transactional
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CmisCwInvoice invoice) {
        invoiceService.updateCwInvoice(invoice);
        return AjaxResult.success();
    }

    /**
     * 删除开票
     */
    @ApiOperation("删除开票")
    @PreAuthorize("@ss.hasPermi('business:invoice:remove')")
    @Log(title = "删除开票", businessType = BusinessType.DELETE)
    @Transactional
    @DeleteMapping("/{invoiceId}")
    public AjaxResult remove(@ApiParam("开票Id") @PathVariable Long invoiceId) {
        invoiceService.deleteById(invoiceId);
        return AjaxResult.success();
    }
}
